package com.springframework.beans.factory.xml;

import com.springframework.beans.factory.config.MyBeanDefinition;
import com.springframework.beans.factory.support.MyBeanDefinitionRegistry;
import org.springframework.beans.factory.parsing.ProblemReporter;
import org.springframework.beans.factory.parsing.ReaderContext;
import org.springframework.beans.factory.parsing.ReaderEventListener;
import org.springframework.beans.factory.parsing.SourceExtractor;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.lang.Nullable;

/**
 * @Author 虎哥
 * @Description //TODO
 * 要带着问题去学习,多猜想多验证
 **/
public class MyXmlReaderContext extends ReaderContext {
    private final MyXmlBeanDefinitionReader reader;

    private final MyNamespaceHandlerResolver namespaceHandlerResolver;


    /**
     * Construct a new {@code XmlReaderContext}.
     * @param resource the XML bean definition resource
     * @param problemReporter the problem reporter in use
     * @param eventListener the event listener in use
     * @param sourceExtractor the source extractor in use
     * @param reader the XML bean definition reader in use
     * @param namespaceHandlerResolver the XML namespace resolver
     */
    public MyXmlReaderContext(
            Resource resource, ProblemReporter problemReporter,
            ReaderEventListener eventListener, SourceExtractor sourceExtractor,
            MyXmlBeanDefinitionReader reader, MyNamespaceHandlerResolver namespaceHandlerResolver) {

        super(resource, problemReporter, eventListener, sourceExtractor);
        this.reader = reader;
        this.namespaceHandlerResolver = namespaceHandlerResolver;
    }


    /**
     * Return the XML bean definition reader in use.
     */
    public final MyXmlBeanDefinitionReader getReader() {
        return this.reader;
    }

    /**
     * Return the bean definition registry to use.
     * @see XmlBeanDefinitionReader#XmlBeanDefinitionReader(BeanDefinitionRegistry)
     */
    public final MyBeanDefinitionRegistry getRegistry() {
        return this.reader.getRegistry();
    }

    /**
     * Return the resource loader to use, if any.
     * <p>This will be non-null in regular scenarios,
     * also allowing access to the resource class loader.
     * @see XmlBeanDefinitionReader#setResourceLoader
     * @see ResourceLoader#getClassLoader()
     */
    @Nullable
    public final ResourceLoader getResourceLoader() {
        return this.reader.getResourceLoader();
    }

    /**
     * Return the bean class loader to use, if any.
     * <p>Note that this will be null in regular scenarios,
     * as an indication to lazily resolve bean classes.
     * @see XmlBeanDefinitionReader#setBeanClassLoader
     */
    @Nullable
    public final ClassLoader getBeanClassLoader() {
        return this.reader.getBeanClassLoader();
    }

    /**
     * Return the environment to use.
     * @see XmlBeanDefinitionReader#setEnvironment
     */
    public final Environment getEnvironment() {
        return this.reader.getEnvironment();
    }

    /**
     * Return the namespace resolver.
     * @see XmlBeanDefinitionReader#setNamespaceHandlerResolver
     */
    public final MyNamespaceHandlerResolver getNamespaceHandlerResolver() {
        return this.namespaceHandlerResolver;
    }


    // Convenience methods to delegate to

    /**
     * Call the bean name generator for the given bean definition.
     * @see XmlBeanDefinitionReader#getBeanNameGenerator()
     * @see org.springframework.beans.factory.support.BeanNameGenerator#generateBeanName
     */
    public String generateBeanName(MyBeanDefinition beanDefinition) {
        return this.reader.getBeanNameGenerator().generateBeanName(beanDefinition, getRegistry());
    }

}
